from PyQt5.QtWidgets import QDialog, QVBoxLayout, QListWidget, QPushButton, QHBoxLayout
from PyQt5.QtWidgets import (
    QWidget,
    QApplication,
    QAbstractItemView,
    QFileDialog,
    QMessageBox,
)
from Config import AppConfig
import os
class WindowUtil:
    def __init__(self,windows):
        self.simple_ivs=[]
        self.map_ivs=[]
        self.selected_index = None
        self.selected_text = None
        self.windows = windows
    def _check_preconditions(self):
        """检查更新前的必要条件"""
        if not AppConfig.root_project_path:
            QMessageBox.warning(self, "配置缺失", "请先选择X1项目路径")
            return False
        if not AppConfig.model_path:
            QMessageBox.warning(self, "配置缺失", "请先选择模型文件")
            return False
        if not os.path.exists(AppConfig.cryption_cpp_file):
            QMessageBox.warning(
                self, "文件缺失", f"未找到加密配置文件: {AppConfig.cryption_cpp_file}"
            )
            return False
        return True
    def on_show_ivs_popup(self):
        """弹出弹窗显示IV列表（使用ListView控件）"""
        if not hasattr(self, 'simple_ivs') or not hasattr(self, 'map_ivs'):
            QMessageBox.warning(self, "数据缺失", "请先加载IV列表")
            return
        
        # 创建对话框
        dialog = QDialog(self.windows)
        dialog.setWindowTitle("IV列表详情")
        dialog.resize(600, 400)
        
        # 创建布局
        layout = QVBoxLayout()
        
        # 创建列表控件
        list_widget = QListWidget()
        
        # 添加IV项到列表控件
        items = []
        for iv in self.simple_ivs:
            items.append(f"[常量] {iv['name']} = {iv['value']}")
        for iv in self.map_ivs:
            items.append(f"[映射] AreaType::{iv['enum']} = {iv['value']}")
        
        list_widget.addItems(items)
        
        # 创建按钮布局
        button_layout = QHBoxLayout()
        
        # 添加确认按钮
        confirm_button = QPushButton("确认")
        confirm_button.clicked.connect(lambda: self._handle_popup_confirm(dialog, list_widget))
        
        # 添加关闭按钮
        close_button = QPushButton("关闭")
        close_button.clicked.connect(dialog.close)
        
        # 设置按钮布局
        button_layout.addWidget(confirm_button)
        button_layout.addWidget(close_button)
        
        # 设置主布局
        layout.addWidget(list_widget)
        layout.addLayout(button_layout)
        dialog.setLayout(layout)
        
        # 显示对话框
        dialog.exec_()
    def _handle_popup_confirm(self, dialog, list_widget):
        """处理弹窗确认按钮点击事件"""
        selected_items = list_widget.selectedItems()
        if not selected_items:
            QMessageBox.warning(self, "选择缺失", "请先选择一个项目")
            return
        
        # 获取选中项的索引
        self.selected_index = list_widget.currentRow()
        self.selected_text = selected_items[0].text()
        
        # 在日志中显示选中项信息
        self.windows.ui.log.append(f"弹窗中选中项: {self.selected_text}")
        self.windows.ui.log.append(f"选中项索引: {self.selected_index}")
        
        # 关闭对话框
        dialog.accept()
        
        # 如果需要进一步处理选中项，可以在这里添加逻辑
        # 例如：根据索引判断是simple_ivs还是map_ivs中的项
        if self.selected_index < len(self.simple_ivs):
            # 选中的是simple_ivs中的项
            selected_iv = self.simple_ivs[self.selected_index]
            self.windows.ui.log.append(f"选中常量IV: {selected_iv['name']}")
        else:
            # 选中的是map_ivs中的项
            map_index = self.selected_index - len(self.simple_ivs)
            selected_iv = self.map_ivs[map_index]
            self.windows.ui.log.append(f"选中映射IV: AreaType::{selected_iv['enum']}")